package classifier;

import java.util.Stack;

public class Orders{
	
	private Node root;
	
	public Orders(Node r){
		root = r;
	}

	
	public void inOrder(){
		Node current = root;
		Stack<Node> stack = new Stack<Node>();
		System.out.println("Inorder: ");
		while (current != null || !stack.isEmpty() ) {
			if ( current != null ){
				stack.push(current);
				current = current.follow(current.getLabel());
								
			}
			else {
				current = stack.pop();
				System.out.println(current.getLabel());
				current = current.follow(current.getLabel());
			}	
		}		
		
	}
	
	public void preOrder(){
		Node current = root;
		Stack<Node> stack = new Stack<Node>();
		System.out.println("Preorder: ");
		while(current!=null || !stack.isEmpty()){
			if(current != null){				
				stack.push(current);
				System.out.println(current.getLabel());
				current = current.follow(current.getLabel());				
			}
			else {
				current = stack.pop();				
				current = current.follow(current.getLabel());
			}
		}
		
	}

	
	public void postOrder(){
		Node current = root;
		Stack<Node> stack = new Stack<Node>();
		System.out.println("Postorder: ");
		
		while(current != null || stack.size() != 0){
			if(current != null){
				stack.push(current);
				current = current.follow(current.getLabel());
			}
			else{				
				current=stack.pop();				
				if(current.getSecondPop()){
					System.out.println(current.getLabel());
					current.setSecondPop(false);	
					current = null;
				}
				else{	
					current.setSecondPop(true);
					stack.push(current);
					current=current.follow(current.getLabel());						
				}
			}
		}	
		
	}
}